package src.四则运算;

import java.util.Random;

public class RationalNumber {
    private int numerator, denominator, temp;

    public RationalNumber()
    {
        Random generator = new Random();
        numerator = generator.nextInt(10) + 1;
        denominator = generator.nextInt(10) + 1;

        if (denominator < numerator)
        {
            temp = denominator;
            denominator = numerator;
            numerator = temp;
        }

        reduce();
    }

    @Override
    public String toString()
    {
        return "(" + numerator + "/" + denominator + ")";
    }

    private void reduce()
    {
        int common = gcd(Math.abs(numerator), denominator);

        numerator = numerator / common;
        denominator = denominator / common;
    }

    private int gcd(int num1, int num2)
    {
        while (num1 != num2)
            if (num1 > num2)
                num1 = num1 - num2;
            else
                num2 = num2 - num1;

        return num1;
    }

}
